<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      :title="t('routes.demo.workFlow.flowTemplateProperty')"
      @register="register"
      @ok="handleSubmit"
      :destroyOnClose="true"
      :minHeight="600"
      :width="500"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { getCreateFlowTemplate } from './flowData';
  import { getFlowTemplateApi } from '/@/api/demo/flow';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';
  import { useRouter } from 'vue-router';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const isUpdate = ref(true);
  const emit = defineEmits(['success', 'register']);
  const { createMessage } = useMessage();
  const webList: any = ref([]);
  const listStep: any = ref([]);
  const form: any = ref(null);
  const useCommonStore = useUserStore();
  const router = useRouter();

  // 定义弹窗是否可见
  const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
    // 重置表单
    webList.value = [];
    listStep.value = [];
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      const res = await getFlowTemplateApi(data.record.id);
      const mainwfdto = res.mainwfdto;
      webList.value = res.weblist;
      listStep.value = res.liststep;
      mainwfdto['bzxzselected'] = res.mainwfdto.bzxzselected === '1';
      mainwfdto['htfsselected'] = res.mainwfdto.htfsselected === '1';
      mainwfdto['classApplicable'] =
        res.mainwfdto.classApplicable.length === 0 ? null : res.mainwfdto.classApplicable;
      setFieldsValue({ ...mainwfdto });
    }
  });

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelCol: { span: 4 },
    wrapperCol: { span: 18 },
    schemas: getCreateFlowTemplate(isUpdate),
    showActionButtonGroup: false,
  });

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      if (unref(isUpdate)) {
        form.value = values;
        form.value['creator'] = useCommonStore.userInfo?.userId;
        form.value['bzxzselected'] = form.value.bzxzselected ? '1' : '0';
        form.value['htfsselected'] = form.value.htfsselected ? '1' : '0';
        let formStr = JSON.stringify(form.value);
        let webListStr = JSON.stringify(webList.value);
        let listStepStr = JSON.stringify(listStep.value);
        router.push({
          name: 'flowModel',
          params: { form: formStr, weblist: webListStr, liststep: listStepStr },
        });
      } else {
        form.value = values;
        form.value['creator'] = useCommonStore.userInfo?.userId;
        form.value['bzxzselected'] = form.value.bzxzselected ? '1' : '0';
        form.value['htfsselected'] = form.value.htfsselected ? '1' : '0';
        form.value['remark'] = form.value.remark === undefined ? '' : form.value.remark;
        form.value['actor'] = form.value.actor === undefined ? 'sa' : form.value.actor;
        form.value['classApplicable'] =
          form.value.classApplicable === undefined ? null : form.value.classApplicable;
        form.value['bzxz'] = form.value.bzxz == undefined ? '1' : form.value.bzxz;
        form.value['htfs'] = form.value.htfs == undefined ? '1' : form.value.htfs;
        form.value['type'] = form.value.type == undefined ? '通知流程' : form.value.type;
        let formStr = JSON.stringify(form.value);
        let webListStr = JSON.stringify(webList.value);
        let listStepStr = JSON.stringify(listStep.value);
        router.push({
          name: 'flowModel',
          params: { form: formStr, weblist: webListStr, liststep: listStepStr },
        });
      }
      closeModal();
      emit('success');
      createMessage.success('成功');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
<style lang="less" scoped>
  ::v-deep(.rc-virtual-list-scrollbar-thumb) {
    visibility: hidden !important;
  }
</style>
